package co.uniquindio.software.medisys.logica;

import java.util.List;
import java.util.Vector;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import co.uniquindio.software.medisys.entidades.Historiaclinica;

public class GestorHistoriaClinica {

	EntityManagerFactory emf = Persistence
			.createEntityManagerFactory("MedisysPU");

	public void guardarHistoriaClinica(Historiaclinica historia) {
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.persist(historia);
		em.flush();
		et.commit();
	}

	public void modificarHistoriaClinica(Historiaclinica historia) {
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		et.begin();
		em.merge(historia);
		em.flush();
		et.commit();
	}

	public Historiaclinica obtenerHistoriaClinicaPaciente(int cedula) {
		Historiaclinica h = null;
		try {
			EntityManager em = emf.createEntityManager();
			h = (Historiaclinica) em.createQuery("SELECT h FROM Historiaclinica h WHERE h.paciente.idPaciente =:cedula")
					.setParameter("cedula", cedula).getSingleResult();
		} catch (NoResultException ex) {
			h = null;
		}
		return h;
	}

}
